/**
 * Created by Brant on 2016/12/28.
 */
module.exports = function ($http,$location,$state,Session,httpservice,$q) {
    var registerService = {};
    registerService.phoneregister = function (phone,pwd,code,phoneRegisttype) {
        //注册
        var params={"registtype":phoneRegisttype,"password":pwd,"cellphone":phone,"cellphoneCode":code};

        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.registerUrl,
            data:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

//邮箱
    registerService.emailregister = function (email,pwd,code,emailRegisttype,msgCode,cellphone) {
        //注册
        var params={"registtype":emailRegisttype,"password":pwd,"cellphone":cellphone,"email":email,"pictureCode":code,"msgCode":msgCode};

        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.registerUrl,
            data:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };
    //获取验证码
    registerService.sendMesCode = function (phoneNum,mode) {
        //获取验证码
        var params={"cellphone":phoneNum,"mode":mode};

        return $http(
            {
                //注册
                url : httpservice.getVerifyCodeUrl,
                method : 'POST',
                data : params,
                dataType : 'json',
                headers : {'Content-Type':'application/json'}
            }
        )
            .then(function (response) {
                return response.data;
            })
            .catch(function (response) {
                console.error('login error', response.status, response.data);
                $("#errormsgId").html("登录失败，失败原因："+response.status);
            })
            .finally(function () {
                /*$("#errormsgId").html("网络链接失败！");*/
            });
    };

    /**
     * 查询是否注册手机号码
     * @returns {Promise}
     */
    registerService.getRegisterPhone = function (mobile) {

        var params={"mobile":mobile};

        var deferred = $q.defer();
        $http({
            method: 'GET',
            url: httpservice.getRegisterUrlPhoneOrEmail,
            params: params
            //headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /**
     * 查询是否注册邮箱
     * @returns {Promise}
     */
    registerService.getRegisterEmail = function (email) {

        var params={"email":email};

        var deferred = $q.defer();
        $http({
            method: 'GET',
            url: httpservice.getRegisterUrlPhoneOrEmail,
            params: params
            //headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /**
     * 邮箱验证码验证
     * @returns {Promise}
     */
    registerService.getEmailRegisterCode = function (code) {

        var params={"pictureCode":code};


        var deferred = $q.defer();
        $http({
            method: 'GET',
            url: httpservice.getEmailRegisterCodeUrl,
            params: params,
            //headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };
    /*发送手机验证码*/
  registerService.sendCode=function(phone,pictureCode) {
    return $http({
      url: httpservice.getVerifyCodeInfoPlusUrl,
      method:"POST",
      headers : {'Content-Type':'application/json'},
      data: {cellphone: phone, mode:6,pictureCode:pictureCode}
    })
  };



    return registerService;
};
